package com.chap16;

/**
 * <p>
 * <code>ThreadInterruptTest</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-24 8:45
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class ThreadInterruptTest2 {
    static int i = 0;

    public static void main(String[] args) {
        System.out.println("begin");
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    i++;
                    System.out.println(i);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        System.out.println("线程被中断抛出中断异常，可以执行退出逻辑");
                        e.printStackTrace();
                        Thread.currentThread().interrupt();
                    }

                    if (i==10){
                        break;
                    }
                }
            }
        });
        t1.start();
        //不会停止线程t1 只会设置一个中断标志位 flag=true
        t1.interrupt();
    }
}


/*
Thread.sleep(5000)会清除中断标记
后面还想用中断标记，需要下catch中重新给线程打中断标记
*/
